package side;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import services.ResearchFile;

public class JacksonStream {


    static ObjectMapper mapper;
/*
 * Zapis do pliku typu JSON
 */
    public static <T> void createJsonFile(String name, ArrayList<ResearchFile> file) throws JsonGenerationException, JsonMappingException, IOException {

        Writer o = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new File(name + ".json")), "UTF8"));
        BufferedWriter out = new BufferedWriter(o);
        try {
            for (ResearchFile r : file) {
                Gson gson = new GsonBuilder()
                        .setPrettyPrinting()
                        .create();
                //    System.out.println(gson.toJson(r));
                //$NON-NLS-1$
                if (file.get(0) == r) {

                    out.write('[');
                }
                out.write(gson.toJson(r));

                if (file.get(file.size() - 1) != r) {
                    out.write(',');
                }
                if (file.get(file.size() - 1) == r) {
                    out.write(']');
                }
                //  System.out.append("zapisano");
            }
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

/*
 *  Tworzenie pliku o nazwie name.json
 */
    static File createFile(String name) {
        File f;
        f = new File(name + ".json");
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //     System.out.println("New file " + name + ".json has been created to the current directory");
        } else {
            f.delete();
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return f;
    }

    public static ArrayList<ResearchFile> readJson(String st) {
        ArrayList<ResearchFile> researchFile = new ArrayList<ResearchFile>();
        try {
            InputStream inputStream = new FileInputStream(st);
            JsonReader reader;
            try {
  if(inputStream == null) {
      side.SideDish.wait(300);
  }
  reader = new JsonReader(new InputStreamReader(inputStream,
                        "UTF-8"));

                JsonElement json = new JsonParser().parse(reader);
                if (json.isJsonArray()) {
                    JsonArray array = json.getAsJsonArray();
                    Gson gson = new Gson();

                    for (JsonElement jsonElement : array) {
                        ResearchFile rf = gson.fromJson(jsonElement, ResearchFile.class);
                        researchFile.add(rf);
                        //        System.out.println("plik należy do" + st + " : "+rf.getName() + "\t" + rf.getSize());
                    }
                } else {
                    System.out.println("Pusty folder!!");
                }
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(listFile.class.getName()).log(Level.SEVERE, null, ex);
            }


        } catch (FileNotFoundException ex) {
            Logger.getLogger(listFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        return researchFile;
    }
}
